﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace QLPM
{
    public partial class BakRes : DevComponents.DotNetBar.Office2007Form
    {
        public BakRes()
        {
            InitializeComponent();
        }

        public void showfile()
        {
            try
            {
                //xoa du lieu dang ton tai tren dieu khien
                
                lvData.Items.Clear();
                DirectoryInfo dir = new DirectoryInfo(Application.StartupPath+"\\Backup\\");
                lvData.Columns.Add("Name", 200, 0);
                lvData.Columns.Add("Size", 70, 0);
                lvData.Columns.Add("Date", 80, 0);

                lvData.View = View.Details;
                ListViewItem item1;
                foreach (FileInfo d in dir.GetFiles("*.mdb"))
                {
                    item1 = new ListViewItem(d.Name);
                    item1.SubItems.Add(d.Length.ToString());
                    item1.SubItems.Add(d.CreationTime.ToShortDateString());
                    lvData.Items.Add(item1);

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btBackup_Click(object sender, EventArgs e)
        {
            string source,destination;
            source = Application.StartupPath;
            Directory.CreateDirectory(source+"\\Backup");
            //string destination;
            if (cbNewName.Checked == false)
            {
                destination = source + "\\Backup\\QLPMT" + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + ".mdb";
                File.Copy(source+"\\QLPMT.mdb", destination, true);//ghi de neu trung ten
                MessageBox.Show("Backup Successfully,Filesave as: " + destination , "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                showfile();
            }
            else
            {
                if (!String.IsNullOrEmpty(txtNewName.Text))
                {
                    destination = source + "\\Backup\\" + txtNewName.Text + ".mdb";
                    File.Copy(source +"\\QLPMT.mdb", destination, true);//ghi de neu trung ten
                    MessageBox.Show("Backup Successfully Filesave as: "+source+"\\Backup\\");
                    showfile();
                }
                else
                    MessageBox.Show("Bạn chưa nhập tên ");
            }
        }

        private void cbNewName_CheckedChanged(object sender, EventArgs e)
        {
            if (cbNewName.Checked)
                txtNewName.Enabled = true;

            if (!cbNewName.Checked)
                txtNewName.Enabled = false;
        }

        private void BakRes_Load(object sender, EventArgs e)
        {
            txtNewName.Enabled = false;
            showfile();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btRestore_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Restore Database :";
                if (MessageBox.Show(msg + "'" + Application.StartupPath + "\\Backup\\" + lvData.FocusedItem.Text + "'", "Thông Báo", MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button3) == DialogResult.Yes)
                {
                        FileInfo d = new FileInfo(Application.StartupPath + "\\Backup\\" + lvData.FocusedItem.Text);
                        string finfo = d.CreationTime.ToShortDateString();
                        //string source = Application.StartupPath + "\\Backup\\" + filename+".mdb";
                        string source = Application.StartupPath + "\\Backup\\" + lvData.FocusedItem.Text;
                        string destination = Application.StartupPath + "\\QLPMT.mdb";
                        File.Copy(source, destination, true);
                        MessageBox.Show("Database đã được restore về ngày : " + finfo + " Successfull!!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                     
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Bạn Chưa chọn file cần Restore", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
                        
        }

        private void btnXoaTapTin_Click(object sender, EventArgs e)
        {
            try
            {           
                string msg = "Bạn có chắc muốn xóa tập tin :";
                if (MessageBox.Show(msg + "'" +Application.StartupPath +"\\Backup\\"+ lvData.FocusedItem.Text + "'", "Thông Báo", MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button3) == DialogResult.Yes)
                {
                    File.Delete(Application.StartupPath + "\\Backup\\" + lvData.FocusedItem.Text);
                    showfile();
                }
            }
            catch (System.Exception )
            {
                MessageBox.Show("Bạn Chưa chọn file cần xóa", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

   
    }
}
